package cn.baiweigang.uu.api.future;

import cn.baiweigang.uu.api.BaseApi;
import cn.baiweigang.uu.enums.*;
import cn.baiweigang.uu.model.BasisInfo;
import cn.baiweigang.uu.model.ContractInfo;
import cn.baiweigang.uu.utils.DateUtil;
import cn.baiweigang.uu.utils.LogUtil;
import com.huobi.future.api.response.market.ContractContractInfoResponse;
import com.huobi.future.api.response.market.MarketHistoryBasisResponse;
import com.huobi.future.api.service.market.MarketAPIServiceImpl;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class MarketFutureApi extends BaseApi {
    private MarketAPIServiceImpl marketAPIService = new MarketAPIServiceImpl();

    public ContractInfo getContractInfo(SymbolEnum symbol, ContractTypeEnum contractType) {
        try {
            ContractContractInfoResponse result =
                    marketAPIService.getContractContractInfo(symbol.getCode(), contractType.getCode(), "");
            if (result != null && result.getData() != null && result.getData().size() > 0) {
                ContractContractInfoResponse.DataBean dataBean = result.getData().get(0);
                ContractInfo contractInfo = new ContractInfo();
                contractInfo.setContractSize(dataBean.getContractSize());
                contractInfo.setContractCode(dataBean.getContractCode());
                contractInfo.setContractType(ContractTypeEnum.byCode(dataBean.getContractType()));
                contractInfo.setCreateDate(dataBean.getCreateDate());
                contractInfo.setDeliveryDate(dataBean.getDeliveryDate());
                contractInfo.setSymbol(SymbolEnum.byCode(dataBean.getSymbol()));
                contractInfo.setSettlementTime(dataBean.getSettlementTime());
                contractInfo.setDeliveryTime(dataBean.getDeliveryTime());
                return contractInfo;
            }
        } catch (Throwable e) {
            LogUtil.logError("获取交割合约信息异常 " + e.getMessage());
        }
        return null;
    }

    public List<BasisInfo> getBasis(ContractCodeEnum contractCode, PeriodEnum period, BasisPriceTypeEnum basisPriceType, int size) {
        try {
            MarketHistoryBasisResponse response = marketAPIService.getMarketHistoryBasis(contractCode.getCode(), period.getCode(), basisPriceType.getCode(), size);
            List<BasisInfo> basisInfoList = new ArrayList<>();
            if (response != null && response.getData() != null && response.getData().size() > 0) {
                for (MarketHistoryBasisResponse.DataBean dataBean : response.getData()) {
                    BasisInfo basisInfo = new BasisInfo();
                    basisInfo.setContractCode(contractCode);
                    basisInfo.setPeriod(period);
                    basisInfo.setBasisPriceType(basisPriceType);
                    basisInfo.setBasis(new BigDecimal(dataBean.getBasis()));
                    basisInfo.setBasisRate(new BigDecimal(dataBean.getBasisRate()));
                    basisInfo.setContractPrice(new BigDecimal(dataBean.getContractPrice()));
                    basisInfo.setIndexPrice(new BigDecimal(dataBean.getIndexPrice()));
                    basisInfo.setId(dataBean.getId());
                    basisInfo.setDateTime(DateUtil.formatDateYMDHMS(new Date(dataBean.getId() * 1000)));

                    basisInfoList.add(basisInfo);
                }
            }
            return basisInfoList;
        } catch (Throwable e) {
            LogUtil.logError("查询基差信息异常 " + e.getMessage());
        }
        return null;
    }
}
